# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================


# ==================================
# Restrict data to LDP candidates only and calculate district-level dispersion 
# in their positions for all districts in the 1986, 1990, and 1993 elections

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

ideal.pre.ldp <- ideal[ideal$pty==1 & ideal$year<1995,]
vars <- by(ideal.pre.ldp, list(ideal.pre.ldp$ku, ideal.pre.ldp$year), 
           function(x) var(x[,5]))

# saved as "LDP_dispersion_by_district.csv"
# renamed first column "ku"

rm(list=ls())









# ==================================
# Merge estimates of the dispersion in LDP candidate positions in each district
# under old system with data on the intensity of IPC faced by all candidates under
# old system:

ldp.vars <- read.csv("LDP_dispersion_by_district.csv")
colnames(ldp.vars)[2:4] <- c("1986", "1990", "1993")
ldp.vars2 <- reshape(ldp.vars,
                     varying=c("1986", "1990", "1993"),
                     v.names="ldp.var",
                     timevar="year",
                     times=c("1986", "1990", "1993"),
                     direction="long")
ldp.vars2$ku_year <- paste(ldp.vars$ku, ldp.vars2$year, sep="_")
rownames(ldp.vars2) <- NULL
ldp.vars3 <- ldp.vars2[complete.cases(ldp.vars2),]
ldp.vars4 <- ldp.vars3[,c(3, 5)]

# read in data on intensity of IPC
load("covars_ideal_points_IPC_preER.Rdata") # saved as ideal
# adds two variables:
# "candidate.count" = number of candidates from the same party in a district
# "IPC_index" = candidate/count/m

# Merge these:
ideal$ku_year <- paste(ideal$ku, ideal$year, sep="_")
ideal.IPC <- merge(ideal, ldp.vars4, by="ku_year", all=T)

# Create a district-level data set with variance in LDP cand
# positions and intensity of IPC:
ideal.IPC.ldp <- ideal.IPC[ideal.IPC$pty==1,]
ideal.IPC.ldp2 <- ideal.IPC.ldp[!(is.na(ideal.IPC.ldp$ldp.var)),]
ideal.IPC.ldp3 <- ideal.IPC.ldp2[, c("year", "prefecture", "ku", "ldp.var", "IPC_index")]
ideal.IPC.ldp4 <- ideal.IPC.ldp3[!duplicated(ideal.IPC.ldp3), ]
nrow(ideal.IPC.ldp4)
save(ideal.IPC.ldp4, file="LDPvars_IPC.Rdata")

rm(list=ls())



# ==================================
# Regression of dispersion in LDP candidate positions in a district and 
# intensity of intra-party competition in that district, pre-ER
# (Table 2 in paper)

load("LDPvars_IPC.Rdata") # saved as ideal.IPC.ldp4
ideal.IPC.ldp4$ku <- as.factor(ideal.IPC.ldp4$ku)
fit1 <- lm(ldp.var ~ IPC_index, data=ideal.IPC.ldp4)
summary(fit1)
fit2 <- lm(ldp.var ~ IPC_index + prefecture + ku, data=ideal.IPC.ldp4)
summary(fit2)

rm(list=ls())




# ==================================
# Why within-party dispersion was higher in 1993
# (page 17-18 in Appendix)

load("covars_ideal_points_IPC_preER.Rdata")

ideal.ldp <- ideal[ideal$pty==1,]
ideal.ldp.90 <- ideal.ldp[ideal.ldp$year==1990,]
ideal.ldp.93 <- ideal.ldp[ideal.ldp$year==1993,]

# Restrict 1993 data to districts that also existed in 1990:
ideal.ldp.93.a <- ideal.ldp.93[ideal.ldp.93$ku %in% ideal.ldp.90$ku,]

# To 1993 data, add a column depicting that district's candidate.count variable
# from 1990:
districts <- unique(ideal.ldp.93.a$ku)
candidate.count.90 <- matrix(as.character(NA), ncol=2, nrow=0)
for(i in 1:length(districts)){
  year.dset <- ideal.ldp.90[ideal.ldp.90$ku==districts[i],]
  ku <- unique(year.dset$ku)
  cc.90 <- unique(year.dset$candidate.count)
  candidate.count.90 <- rbind(candidate.count.90, cbind(ku, cc.90))
}
ideal.ldp.93.b <- merge(ideal.ldp.93.a, candidate.count.90, by="ku")
colnames(ideal.ldp.93.b)[13] <- "candidate.count.90"
ideal.ldp.93.b$candidate.count.90 <- as.numeric(as.character(ideal.ldp.93.b$candidate.count.90))

# Calculate dispersion in LDP candidate positions among the group of 
# LDP candidates who faced similar levels of intra-party competition in both
# elections:
mer.same <- ideal.ldp.93.b[ideal.ldp.93.b$candidate.count==ideal.ldp.93.b$candidate.count.90,]
var(mer.same$sep_theta)

# Calculate dispersion in LDP candidate positions among the group of 
# LDP candidates who faced lower levels of intra-party competition in 1993:
mer.lower <- ideal.ldp.93.b[ideal.ldp.93.b$candidate.count<ideal.ldp.93.b$candidate.count.90,]
var(mer.lower$sep_theta)

rm(list=ls())



